package org.bozo.im.service.protocols.xmpp.stanza.task.session;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.generator.StanzaGenerator;
import org.bozo.im.service.protocols.xmpp.stanza.task.Task;
import org.bozo.im.service.protocols.xmpp.stanza.task.roster.RosterTask;
import org.bozo.im.service.protocols.xmpp.util.namespace.Namespace;

import android.util.Log;

public class SessionTask extends Task {

	public SessionTask(String pId) {
		super(pId);
	}

	@Override
	public void doTask(XMPPSession pSession, StringBuilder pBuilder) throws Exception {

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.task.session.SessionTask.doTask ---------- Starting Session negociation");
		// ---------------------------------------------------------------------------------------
	
		StanzaGenerator lSessionStanza = new StanzaGenerator(Stanza.SESSION_SESSION);
		lSessionStanza.setDefaultNamespace(Namespace.SESSION);
		
		StanzaGenerator lIQStanza = new StanzaGenerator(Stanza.JABBER_CLIENT_IQ);
		lIQStanza.setAttribute(Namespace.JABBER_CLIENT, "to", pSession.getConfiguration().getTo());
		lIQStanza.setAttribute(Namespace.JABBER_CLIENT, "type", "set");
		lIQStanza.setAttribute(Namespace.JABBER_CLIENT, "id", getId());
		lIQStanza.setDefaultNamespace(Namespace.JABBER_CLIENT);
		lIQStanza.addChild(lSessionStanza);
		
		lIQStanza.toXml(pBuilder);
		
		// Let's retrieve the roster once we are logged in
		pSession.getTaskProcessor().pushTask(new RosterTask(pSession.getId()), getId());
	}

}
